#include "task.h"
#include "taskmanager.h"

namespace tinkle {

uint32_t Task::_unique_id_seed = 0;

Task::Task(BaseTask *parent)
    : BaseTask(this, parent),
      _state(TASK_INIT),
      _hasNetworkEvent(false) {
    Initialize();
}

void Task::Initialize() {
    _id = _unique_id_seed++;
    if (_unique_id_seed == 0xFFFFFFFF) {
        _unique_id_seed = 0;
    }
}


Task::~Task() {
    // pass
}

uint32_t Task::Id() const {
    return _id;
}

bool Task::IsDone() const {
    return (_state == TASK_DONE) ? true : false;
}

void Task::Start() {
    if (_state != TASK_INIT) {
        return;
    }
    _state = TASK_READY;
    // all task should run via the task manager
    GetManager()->StartTask(this);
}

void Task::Process() {
    if (_state == TASK_INIT) {
        ProcessInit();
        _state = TASK_READY;
    }
    ProcessReady();
}

bool Task::HasNetworkEvent() const {
    return false;
}

void Task::ProcessInit() {
    // do nothing
}

void Task::ProcessReady() {
    // do nothing
}

}